import Vue from "vue"
import VueRouter from "vue-router"
import routes from "routes/routes"
import {defined} from "./defined"

//处理重复触发编程式导航的bug
//获取原型对象上的push函数
const originalPush = VueRouter.prototype.push
//修改原型对象中的push方法
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch(err => err)
}

Vue.use(VueRouter)

const router = new VueRouter({
  routes,
  mode:"history",
  scrollBehavior(to){
    // console.log(to);
    // from 从哪个组件来
    // to 去哪个组件
    let arr = ["info","rule","after","ratings","phone"]
    if(arr.includes(to.name)){
      if(to.name==='info'&&to.redirectedFrom){
        return {x:0,y:0}
      }
    }else{
      return {x:0,y:0}
    }
  },
  linkActiveClass:"active"
})

defined(router)
export default router